<template>
  <div>
    <div class="flex flex-col h-full">
      <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
        <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
          <a-form-item name="pyjm" label="" class="w-72">
            <a-input v-model:value="formState.pyjm" placeholder="排班编号/班制名称/助记码" />
          </a-form-item>
        </a-form>
      </SearchContainer>
      <a-card class="mt-2 flex-1">
        <div class="mb-3">
          <a-button type="primary" @click="onClickAdd()">新增</a-button>
          <!-- <a-button class="ml-3" @click="onExport">导出</a-button> -->
        </div>
        <BaseTable :loading="loading" :data-source="tableData" :pagination="pagination" bordered fill column-code="000248">
          <!-- -->
          <template #bodyCell="{ column, record, index }">
            <template v-if="column.dataIndex === 'xh'">
              {{ index + 1 }}
            </template>
            <template v-if="column.dataIndex === 'status'">
              <a-switch v-model:checked="record.status" @change="onChangeSwitch(record)" />
            </template>
            <template v-if="column.dataIndex === 'schdName'">
              <PopoverTable :width="1000" :config="detailConfig" :params="{ schdCode: record.schdCode }">
                {{ record?.schdName }}
              </PopoverTable>
            </template>
            <template v-if="column.dataIndex === 'action'">
              <a-button type="link" size="small" :disabled="!record.status" @click="onClickAdd(record)">修改</a-button>
              <a-divider type="vertical" />
              <BaseButton confirm-text="确定删除?" type="text" danger size="small" @click="onClickDelete(record)"> 删除 </BaseButton>
            </template>
          </template>
        </BaseTable>
      </a-card>
    </div>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData()" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import Edit from './Edit.vue'

const rowData = ref(null)
const editOpen = ref(false)
const formRef = ref(null)
const formState = ref({})

const detailConfig = ref({
  url: '/operation/scheduling/getSchedulingTimeList',
  columnCode: '000249',
  rowKey: 'id',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      NO: index + 1
    }))
  }
})

const onClickAdd = (record = {}) => {
  rowData.value = { ...record }
  editOpen.value = true
}

const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/scheduling/getSchedulingList',
  immediate: true,
  params: formState.value,
  formatResult: (res) => {
    const list = res.map((item) => {
      return {
        ...item,
        status: item.status ? false : true
      }
    })
    return list
  }
})

const onClickDelete = async (record) => {
  record.status = record.status ? 0 : 1
  const res = await post('/operation/scheduling/doDelScheduling', record)
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
const onChangeSwitch = async (record) => {
  record.status = record.status ? 0 : 1
  const res = await post('/operation/scheduling/doEnableDisableSchedulingDict', record)
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
